﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MakeIT.Deo.Negocios;
using System.IO;

using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;


namespace MakeIT.Deo.Apresentacao
{
    public partial class teste2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int idProduto = int.Parse(Request["idProduto"]);
            string tipo = Request["type"];
            if (tipo==null) tipo="small";

            //Response.ContentType = "image/jpeg";

            //Recupera dados
            ProdutoBO produtoBO = new ProdutoBO();
            produto produtoVO = produtoBO.RecuperaProduto(idProduto);

            if (produtoVO != null)
            {
                if (tipo.Equals("large") && produtoVO.Fotog != null)
                {

                    System.Drawing.Image imagem = byteArrayToImage(produtoVO.Fotog);
                    System.Drawing.Image imagem2 = FixedSize(imagem, 200, 200); //388 x 520

                    Response.ContentType = "image/jpeg";
                    imagem2.Save(Response.OutputStream, ImageFormat.Jpeg);
                    //Response.ContentType = produtoVO.MimeG;
                    //Response.BinaryWrite(produtoVO.Fotog);
                    return;

                }
                if ((tipo.Equals("small") && produtoVO.Fotop != null) || (tipo.Equals("large") && produtoVO.Fotog == null && produtoVO.Fotop != null))
                {
                    Response.ContentType = produtoVO.MimeP;
                    Response.BinaryWrite(produtoVO.Fotop);
                    return;
                }
            }

            Response.ContentType = "image/png";
            string filePath = HttpContext.Current.Server.MapPath("~/App_Themes/Padrao/imagens/masterPage/SemFoto.png");
            Response.BinaryWrite(File.ReadAllBytes(filePath));
        }


        public byte[] imageToByteArray(System.Drawing.Image imageIn)
        {
            MemoryStream ms = new MemoryStream();
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            return ms.ToArray();
        }
        public System.Drawing.Image byteArrayToImage(byte[] byteArrayIn)
        {
            MemoryStream ms = new MemoryStream(byteArrayIn);
            System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
            return returnImage;
        }

        static System.Drawing.Image FixedSize(System.Drawing.Image imgPhoto, int Width, int Height)
        {
            int sourceWidth = imgPhoto.Width;
            int sourceHeight = imgPhoto.Height;
            int sourceX = 0;
            int sourceY = 0;
            int destX = 0;
            int destY = 0;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)Width / (float)sourceWidth);
            nPercentH = ((float)Height / (float)sourceHeight);
            if (nPercentH < nPercentW)
            {
                nPercent = nPercentH;
                destX = System.Convert.ToInt16((Width -
                              (sourceWidth * nPercent)) / 2);
            }
            else
            {
                nPercent = nPercentW;
                destY = System.Convert.ToInt16((Height -
                              (sourceHeight * nPercent)) / 2);
            }

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap bmPhoto = new Bitmap(Width, Height,
                              PixelFormat.Format24bppRgb);
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
                             imgPhoto.VerticalResolution);

            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            grPhoto.Clear(Color.Red);
            grPhoto.InterpolationMode =
                    InterpolationMode.HighQualityBicubic;

            grPhoto.DrawImage(imgPhoto,
                new Rectangle(destX, destY, destWidth, destHeight),
                new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
                GraphicsUnit.Pixel);

            grPhoto.Dispose();
            return bmPhoto;
        }
    
    }
}
